home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- **
- ** Project Name: DropShell
- ** File Name: DropShell.c
- **
- ** Description: Main application code for the QuickShell
- **
- *******************************************************************************
- ** A U T H O R I D E N T I T Y
- *******************************************************************************
- **
- ** Initials Name
- ** -------- -----------------------------------------------
- ** LDR Leonard Rosenthol
- ** MTC Marshall Clow
- ** SCS Stephan Somogyi
- **
- ******************************************************************************/
- #include "GifScan.h"
-
- #include "DSGlobals.h"
- #include "DSUserProcs.h"
- #include "DSAppleEvents.h"
-
- #include "DropShell.h"
-
- #include "Prefs.h"
- #include "GSDrag.h"
-
- Boolean gDone, gOApped, gHasAppleEvents, gCmdHeldDown, gInBackground, gHasDrag;
- extern thePrefsHandle gPrefs;
-
- void InitToolbox (void)
- {
- MoreMasters ();
- MoreMasters ();
- MoreMasters ();
- MoreMasters ();
-
- InitGraf ( &qd.thePort );
- InitFonts ();
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (nil); // use of ResumeProcs no longer approved by Apple
- InitCursor ();
- FlushEvents ( everyEvent, 0 );
- }
-
- // Let's setup those global variables that the DropShell uses.
- // If you add any globals for your own use,
- // init them in the InitUserGlobals routine in DSUserProcs.c
-
- Boolean InitGlobals(void)
- {
- long aLong;
-
- gDone = false;
- gOApped = false; // probably not since users are supposed to DROP things!
- gHasAppleEvents = Gestalt ( gestaltAppleEventsAttr, &aLong ) == noErr;
- gHasDrag = IsDragManagerInstalled();
-
- return(InitUserGlobals()); // call the user proc
- }
-
- // Again, nothing fancy. Just setting up the menus.
- // If you add any menus to your DropBox - insert them here!
-
-
- void SetUpMenus (void)
- {
- MenuHandle menu;
- Handle menuBar;
-
- menuBar = GetNewMBar(128);
- if ( menuBar == nil ) ErrorAlert(kErrStringID, eMemError, 12900, true);
-
- SetMenuBar(menuBar);
- DisposeHandle(menuBar);
-
- menu = GetMenuHandle(kAppleNum);
- AddResMenu(menu, 'DRVR');
-
- DrawMenuBar ();
- }
-
-
- // --------------- Standard Event Handling routines ----------------------
-
- void ShowAbout ( void )
- {
- (void) Alert ( 128, nil );
- }
-
- void DoMenu ( long retVal )
- {
- short menuID, itemID;
- Str255 itemStr;
- OSErr saveErr;
- TEHandle te;
- Handle aHandle;
- long oldSize, newSize;
- long total, contig;
-
- menuID = HiWord ( retVal );
- itemID = LoWord ( retVal );
-
- switch ( menuID )
- {
- case kAppleNum:
- if ( itemID == 1 )
- ShowAbout (); // Show the about box
- else
- {
- GetMenuItemText(GetMenuHandle(kAppleNum), itemID, itemStr);
- OpenDeskAcc(itemStr);
- }
- break;
-
- case kFileNum:
- switch (itemID)
- {
- case iSelectFile:
- SelectFile(); // call file selection userProc
- break;
-
- case iShowHide: // Show and Hide window
- if ( gOutWindow != nil )
- {
- if ( FrontWindow() != nil )
- HideWindow(gOutWindow);
- else
- ShowWindow(gOutWindow);
- SetCursor(&qd.arrow);
- }
- break;
-
- case iSaveAs: // Save file
- HandleTextFileSave();
- break;
-
- case iPreferences: // Preferences
- SetPreferences();
- break;
-
- case iQuit:
- SendQuitToSelf(); // send self a 'quit' event
- break;
-
- default:
- break;
- }
- break;
-
- case kEditNum:
- if ( !SystemEdit(menuID-1) )
- {
- te = ((DocumentPeek)FrontWindow())->docTE;
- switch ( itemID )
- {
- case iCut: // Cut
- if ( ZeroScrap() == noErr )
- {
- PurgeSpace(&total, &contig);
- if ((*te)->selEnd - (*te)->selStart + kTESlop > contig)
- SysBeep(20);
- else
- {
- TECut(te);
- if ( TEToScrap() != noErr )
- {
- SysBeep(20);
- ZeroScrap();
- }
- }
- }
- break;
- case iCopy: // Copy
- if ( ZeroScrap() == noErr )
- {
- TECopy(te);
- if ( TEToScrap() != noErr )
- {
- SysBeep(20);
- ZeroScrap();
- }
- }
- break;
-
- case iPaste: // Paste
- if ( TEFromScrap() == noErr )
- {
- if ( TEGetScrapLen() + ((*te)->teLength -
- ((*te)->selEnd - (*te)->selStart)) > kMaxTELength )
- SysBeep(20);
- else
- {
- aHandle = (Handle) TEGetText(te);
- oldSize = GetHandleSize(aHandle);
- newSize = oldSize + TEGetScrapLen() + kTESlop;
- SetHandleSize(aHandle, newSize);
- saveErr = MemError();
- SetHandleSize(aHandle, oldSize);
- if (saveErr != noErr)
- SysBeep(20);
- else
- TEPaste(te);
- }
- }
- else
- SysBeep(20);
- break;
-
- case iClear: // Clear
- TEDelete(te);
- break;
-
- case iSelectAll: // Select All
- TESetSelect(0, kMaxTELength, te);
- break;
- }
- AdjustScrollbars(FrontWindow(), false);
- AdjustTE(FrontWindow());
- }
- break;
-
- default:
- break;
-
- }
- HiliteMenu(0); // turn it off!
- }
-
- void DoMouseDown ( EventRecord *curEvent )
- {
- WindowRef whichWindow;
- short whichPart;
- Point mouse;
- DocumentPeek doc;
-
- whichPart = FindWindow ( curEvent->where, &whichWindow );
- switch ( whichPart )
- {
- case inMenuBar:
- DoMenu( MenuSelect ( curEvent->where ));
- break;
-
- case inSysWindow:
- SystemClick( curEvent, whichWindow );
- break;
-
- case inContent:
- if ( whichWindow != FrontWindow() )
- {
- SelectWindow(whichWindow);
- }
- else
- {
- if ( whichWindow == gOutWindow )
- {
- doc = (DocumentPeek)whichWindow;
- mouse = curEvent->where;
- GlobalToLocal(&mouse);
-
- // Stuff to handle drags
- if ( gHasDrag )
- {
- TEGetHiliteRgn(doc->docHiliteRgn, doc->docTE);
- if ( PtInRgn(mouse, doc->docHiliteRgn) )
- {
- HandleDragSelection(curEvent, doc->docHiliteRgn);
- }
- else
- {
- DoContentClick(gOutWindow, curEvent);
- }
- }
- else
- {
- DoContentClick(gOutWindow, curEvent);
- }
- }
- }
- break;
-
- case inDrag:
- {
- Rect boundsRect = (*GetGrayRgn())->rgnBBox;
-
- DragWindow ( whichWindow, curEvent->where, &boundsRect );
- AdjustDragRgn();
- }
- break;
-
- case inGoAway:
- if ( whichWindow == gOutWindow )
- {
- if ( TrackGoAway(gOutWindow, curEvent->where) )
- {
- HideWindow(gOutWindow);
- AdjustDragRgn();
- }
- }
- break;
-
- case inZoomIn:
- case inZoomOut:
- if ( whichWindow == gOutWindow )
- {
- if( TrackBox(gOutWindow, curEvent->where, whichPart) )
- {
- DoZoomWindow(gOutWindow, whichPart);
- AdjustDragRgn();
- }
- }
- break;
-
- case inGrow:
- if ( whichWindow == gOutWindow )
- {
- DoGrowWindow(gOutWindow, curEvent);
- AdjustDragRgn();
- }
- break;
-
- default:
- break;
- }
- }
-
-
-
- void DoKeyDown(EventRecord *event)
- {
- WindowPtr window;
- char key;
- TEHandle te;
-
- window = FrontWindow();
- if ( IsAppWindow(window) )
- {
- te = ((DocumentPeek)window)->docTE;
- key = event->message & charCodeMask;
-
- if ( key == kDelChar ||
- (*te)->teLength - ((*te)->selEnd - (*te)->selStart) + 1 < kMaxTELength )
- {
- TEKey(key, te);
- AdjustScrollbars(window, false);
- AdjustTE(window);
- }
- else
- SysBeep(20);
- }
- }
-
- void DoUpdate(WindowRef window)
- {
- GrafPtr savePort;
- RGBColor theHiliteColor, theUsedColor;
- DocumentPeek doc;
- PenState thePnState;
-
- if ( IsAppWindow(window) )
- {
- BeginUpdate(window);
- if ( ! EmptyRgn(window->visRgn) )
- {
- DrawWindow(window);
- AdjustDragRgn();
- }
- EndUpdate(window);
- }
- }
-
- void DoActivate(WindowRef window, Boolean becomingActive)
- {
- RgnHandle tempRgn, clipRgn;
- Rect growRect;
- DocumentPeek doc;
-
- if ( IsAppWindow(window) )
- {
- doc = (DocumentPeek) window;
- if ( becomingActive )
- {
- tempRgn = NewRgn();
- clipRgn = NewRgn();
- GetLocalUpdateRgn(window, tempRgn);
- GetClip(clipRgn);
- DiffRgn(clipRgn, tempRgn, tempRgn);
- SetClip(tempRgn);
- TEActivate(doc->docTE);
- SetClip(clipRgn);
- DisposeRgn(tempRgn);
- DisposeRgn(clipRgn);
-
- (*doc->docVScroll)->contrlVis = kControlVisible;
- (*doc->docHScroll)->contrlVis = kControlVisible;
- InvalRect(&(*doc->docVScroll)->contrlRect);
- InvalRect(&(*doc->docHScroll)->contrlRect);
-
- growRect = window->portRect;
-
- growRect.top = growRect.bottom - kScrollbarAdjust;
- growRect.left = growRect.right - kScrollbarAdjust;
- InvalRect(&growRect);
-
- if ( gHasDrag ) TEGetHiliteRgn(doc->docHiliteRgn, doc->docTE);
- }
- else
- {
- TEDeactivate(doc->docTE);
-
- HideControl(doc->docVScroll);
- HideControl(doc->docHScroll);
-
- DrawGrowIcon(window);
- }
- AdjustDragRgn();
- }
- }
-
- static void AdjustCursor(void)
- {
- WindowPtr window;
- RgnHandle iBeamRgn, dragArrowRgn;
- Rect iBeamRect;
- Point mouse;
-
- window = FrontWindow();
- if ( (!gInBackground) && ( !IsDAWindow(window) ) )
- {
- if ( IsAppWindow(window) )
- {
- SetPort(window);
-
- GetMouse(&mouse);
-
- iBeamRgn = NewRgn();
- iBeamRect = (*((DocumentPeek)window)->docTE)->viewRect;
- RectRgn(iBeamRgn, &iBeamRect);
-
- if ( gHasDrag )
- {
- // TEGetHiliteRgn get the region handle in LOCAL points
- dragArrowRgn = NewRgn();
- TEGetHiliteRgn(dragArrowRgn, ((DocumentPeek)window)->docTE);
-
- if ( PtInRgn(mouse, dragArrowRgn) )
- {
- SetCursor(&qd.arrow);
- }
- else if ( PtInRgn(mouse, iBeamRgn) )
- {
- SetCursor(*GetCursor(iBeamCursor));
- }
- else
- {
- SetCursor(&qd.arrow);
- }
- DisposeRgn(dragArrowRgn);
- }
- else
- {
- if ( PtInRgn(mouse, iBeamRgn) )
- {
- SetCursor(*GetCursor(iBeamCursor));
- }
- else
- {
- SetCursor(&qd.arrow);
- }
- }
- DisposeRgn(iBeamRgn);
- }
- }
- }
-
- static void DoIdle(void)
- {
- WindowPtr window;
-
- window = FrontWindow();
- if ( IsAppWindow(window) )
- {
- TEIdle(((DocumentPeek) window)->docTE); // Blink the caret
- AdjustCursor(); // Always
- }
- }
-
- static void AdjustMenus(void)
- {
- StringHandle itemString;
- TEHandle teh;
- static MenuRef fileMenu, editMenu;
-
- fileMenu = GetMenuHandle(kFileNum);
- editMenu = GetMenuHandle(kEditNum);
- if ( FrontWindow() != nil )
- {
- itemString = GetString(130);
- SetMenuItemText(fileMenu, 2, itemString[0]);
- SetItemCmd(fileMenu, 2, 'W');
-
- teh = ((DocumentPeek)gOutWindow)->docTE;
- if ( ((*teh)->selEnd - (*teh)->selStart) == 0 )
- {
- DisableItem(editMenu, iCut);
- DisableItem(editMenu, iCopy);
- DisableItem(editMenu, iClear);
- }
- else
- {
- EnableItem(editMenu, iCut);
- EnableItem(editMenu, iCopy);
- EnableItem(editMenu, iClear);
- }
-
- if ( TEFromScrap() )
- {
- if ( !TEScrapHandle() )
- DisableItem(editMenu, iPaste);
- else
- EnableItem(editMenu, iPaste);
- }
- }
- else
- {
- itemString = GetString(129);
- SetMenuItemText(fileMenu, 2, itemString[0]);
- SetItemCmd(fileMenu, 2, 'K');
- }
- }
-
- void AdjustDragRgn(void)
- {
- DragInfoHandle dragInfo;
- Rect receiveRect;
-
- if ( gHasDrag )
- {
- dragInfo = (DragInfoHandle)GetWRefCon(gOutWindow);
- receiveRect = gOutWindow->portRect;
- receiveRect.bottom -= kScrollbarAdjust;
- receiveRect.right -= kScrollbarAdjust;
- RectRgn((*dragInfo)->receiveRgn, &receiveRect);
- }
- }
-
- void main ( void )
- {
- char key;
- EventRecord event;
-
- InitToolbox ();
-
- gInBackground = false;
-
- if ( InitGlobals () )
- {
- // if we succeeding in initting self
- if ( !gHasAppleEvents )
- {
- ErrorAlert ( kErrStringID, kCantRunErr, 0, true );
- }
- else
- {
- InitAEVTStuff ();
- SetUpMenus ();
-
- while ( !gDone )
- {
- if ( WaitNextEvent (everyEvent, &event, 0, nil) )
- {
- switch ( event.what )
- {
- case kHighLevelEvent:
- DoHighLevelEvent ( &event );
- break;
-
- case mouseDown:
- DoMouseDown ( &event );
- break;
-
- case keyDown:
- case autoKey:
- key = event.message & charCodeMask;
- if ( event.modifiers & cmdKey )
- {
- if ( event.what == keyDown )
- {
- DoMenu(MenuKey(key));
- }
- }
- else
- DoKeyDown(&event);
- break;
-
- case kOSEvent:
- {
- switch ((event.message >> 24) & 0x0FF)
- {
- case kMouseMovedMessage:
- DoIdle();
- break;
-
- case kSuspendResumeMessage:
- gInBackground = (event.message & kResumeMask) == 0;
- DoActivate(FrontWindow(), !gInBackground);
- break;
- }
- }
- break;
-
- case diskEvt:
- if (HiWord(event.message))
- {
- Point diskInitPt;
-
- diskInitPt.v = diskInitPt.h = 100;
- DILoad();
- DIBadMount(diskInitPt, event.message);
- DIUnload();
- }
- break;
-
- case updateEvt:
- DoUpdate((WindowRef)(event.message));
- break;
-
- case activateEvt:
- DoActivate((WindowRef)(event.message), (event.modifiers & activeFlag) != 0);
- break;
- }
- AdjustCursor();
- AdjustMenus();
- }
- else
- {
- DoIdle();
- }
- }
- }
- }
- DisposeUserGlobals(); // call the userproc to clean itself up
- }